﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;

namespace DAL_V1.App_Code.DBBroker
{
    public class DepartmentDBBroker
    {
        private SSISDataSet ds;
        private SSISDataSetTableAdapters.DepartmentTableAdapter tda;
        private int index;
        private List<Department> dept_list;
        private Department dept;
        private SSISDataSet.DepartmentDataTable dept_td;

        //singleton
        private static bool instance_flag = false;
        private static DepartmentDBBroker instance;


        private DepartmentDBBroker()
        {
            ds = new SSISDataSet();
            tda = new SSISDataSetTableAdapters.DepartmentTableAdapter();
            tda.Fill(ds.Department);
        }

        public static DepartmentDBBroker getDepartmentDBBroker()
        {
            if (!instance_flag)
            {
                instance = new DepartmentDBBroker();
                instance_flag = true;
                return instance;
            }
            else
            {
                return instance;
            }
        }

        public List<Department> selectAllDepartment()
        {
            dept_list = new List<Department>();
            
            foreach (SSISDataSet.DepartmentRow r in ds.Department.Rows)
            {
                dept = new Department();
                dept.DeptId = r.DeptId;
                dept.DeptName = r.DeptName;
                dept.ContactName = r.ContactName;
                dept.PhNo = r.PhNo;
                dept.FaxNo = r.FaxNo;
                dept.DepHeadId = r.DeptHeadId;
                dept.CollectionId = r.CollectionId;
                dept.RepresentativeId = r.RepresentativeId;

                //Employee
                List<Employee> empe_list = new List<Employee>();
                empe_list = EmployeeDBBroker.getEmployeeDBBroker().selectEmployeeByDept(r.DeptId);
                dept.Empe_list = empe_list;

                dept_list.Add(dept);
            }

            return dept_list;
        }

        public DataTable selectAllDepartment_DT()
        {
            return ds.Department;
        }

        public int addDepartment(Department dept)
        {
            SSISDataSet.DepartmentRow newRow = ds.Department.NewDepartmentRow();
            newRow.DeptId = dept.DeptId;
            newRow.DeptName = dept.DeptName;
            newRow.ContactName = dept.ContactName;
            newRow.PhNo = dept.PhNo;
            newRow.FaxNo = dept.FaxNo;
            newRow.DeptHeadId = dept.DepHeadId;
            newRow.CollectionId = dept.CollectionId;
            newRow.RepresentativeId = dept.RepresentativeId;
                     
            ds.Department.Rows.Add(newRow);
            return tda.Update(ds.Department);
        }

        //private int getIndex(String Id)
        //{
        //    SSISDataSet.DepartmentRow row = ds.Department.FindByDeptId(Id);
        //    return ds.Department.Rows.IndexOf(row);
        //}

        private SSISDataSet.DepartmentRow getRowById(String Id)
        {
            return ds.Department.FindByDeptId(Id);
        }

        public int updateDepartment(Department dept, String Id)
        {
            SSISDataSet.DepartmentRow row = getRowById(Id);
            if (row != null)
            {
                row.DeptId = dept.DeptId;
                row.DeptName = dept.DeptName;
                row.ContactName = dept.ContactName;
                row.PhNo = dept.PhNo;
                row.FaxNo = dept.FaxNo;
                row.DeptHeadId = dept.DepHeadId;
                row.CollectionId = dept.CollectionId;
                row.RepresentativeId = dept.RepresentativeId;
            }

            return tda.Update(ds.Department);
        }

        public int deleteDepartment(String Id)
        {
            SSISDataSet.DepartmentRow row = getRowById(Id);
            if (row != null)
            {
                ds.Department.Rows.Remove(row);
            }
            return tda.Update(ds.Department);
        }

        public Department selectDepartment(String Id)
        {
            SSISDataSet.DepartmentRow r = getRowById(Id);
            if (r != null)
            {
                dept = new Department();
                dept.DeptId = r.DeptId;
                dept.DeptName = r.DeptName;
                dept.ContactName = r.ContactName;
                dept.PhNo = r.PhNo;
                dept.FaxNo = r.FaxNo;
                dept.DepHeadId = r.DeptHeadId;
                dept.CollectionId = r.CollectionId;
                dept.RepresentativeId = r.RepresentativeId;

                //Employee
                List<Employee> empe_list = new List<Employee>();
                empe_list = EmployeeDBBroker.getEmployeeDBBroker().selectEmployeeByDept(r.DeptId);
                dept.Empe_list = empe_list;

                return dept;
            }
            else
            {
                return null;
            }
        }
    }
}
